/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.article;

import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.article.ImageDetails;

/**
 *
 * @author Joris
 */
@Stateless
public class ImageFacade extends AbstractFacade<Image> implements ImageFacadeLocal {
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;
    
    @EJB
    private ModelFacadeLocal modelFacade;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ImageFacade() {
        super(Image.class);
    }
    
    @Override
    public Image createImage(ImageDetails id, Article a, Model m){
        Image img = new Image();
        img.setImage(id.getImage());
        img.setArticleid(a);
        img.setModelid(m);
        create(img);
        return img;
    }
    
    @Override
    public ImageDetails convert(Image i){
        if(i == null)
            return null;
        return new ImageDetails(i.getId(), i.getImage(), modelFacade.convert(i.getModelid()));
    }
    
    @Override
    public Collection<ImageDetails> convert (Collection<Image> images){
        Collection<ImageDetails> details = new ArrayList<ImageDetails>();
        for(Image image : images){
            details.add(convert(image));
        }
        return details;
    }
    
}
